import UtilsTemplate from '../../../components/UtilsTemplate';
import Worker from '@/util/worker/Worker';
import Pool from '@/util/worker/Pool';
export default {
	extends:UtilsTemplate,
	data(){
		return {
			apis:[
				{
					name:'工作线程',
					info:'新增一个工作线程运行指定文件, _task属性表示正在执行的任务数, completed属性在完成所有任务时调用',
					code:`
<script>
//在需要worker线程运行的文件里 如: myworker.worker.js
import expose from '@lexmis/baseui/src/util/worker/expose';

expose({
	add(a,b){
		return a+b;
	},
	multi(a,b){
		return a*b;
	}
});

//在需要的地方创建线程
import Worker from '@lexmis/baseui/src/util/worker/Worker';

const workExecutor=Worker(require('./myworker.worker.js'));
workExecutor.then(async({add,multi}) => {
	await add(2,3); //5
	await new Promise(function(resolve){
		setTimeout(resolve,1000);
	});
	await multi(2,3); //6
}).then(() => {
	console.log('任务完成1!');
});
</${'script'}>`,
					tabs:[
						{
							name:'expose(obj) => void',
							args:[
								{name:'obj',info:'暴露的方法映射',type:'ExposeFns',def:''},
							]
						},{
							name:'ExposeFns',
							args:[
								{name:'[key:string]',info:'暴露的方法',type:'function',def:''},
							]
						},{
							name:'Worker(module) => Promise<ExposeFns>',
							args:[
								{name:'module',info:'require方法返回的module',type:'Module',def:''},
							]
						}
					]
				},{
					name:'工作线程池',
					info:'为运行指定文件生成一个线程池',
					code:`
<script>
//在需要worker线程运行的文件里 如: myworker.worker.js
import expose from '@lexmis/baseui/src/util/worker/expose';

expose({
	add(a,b){
		return a+b;
	},
	multi(a,b){
		return a*b;
	}
});

//在需要的地方创建线程池
import Pool from '@lexmis/baseui/src/util/worker/Pool';

const poolExecutor=Pool(require('./myworker.worker.js'));
poolExecutor(async({add,multi}) => {
	await add(2,3); //5
	await new Promise(function(resolve){
		setTimeout(resolve,1000);
	});
	await multi(2,3); //6
}).then(() => {
	console.log('任务完成2!');
});
</${'script'}>`,
					tabs:[
						{
							name:'Pool(module) => PoolExecutor',
							args:[
								{name:'module',info:'require方法返回的module',type:'Module',def:''},
							]
						},{
							name:'PoolExecutor(task) => Promise',
							args:[
								{name:'task',info:'执行的任务',type:'function',def:''},
							]
						}
					]
				}
			],
		};
	},
};
(async function(){
	console.log('=========工作线程=========');
	const workExecutor=Worker(require('./myworker.worker.js'));
	await workExecutor.then(async({add,multi}) => {
		console.log(await add(2,3));
		await new Promise(function(resolve){
			setTimeout(resolve,1000);
		});
		console.log(await multi(2,3));
	}).then(() => {
		console.log('任务完成1!');
	});
	console.log('=========工作线程池=========');
	const poolExecutor=Pool(require('./myworker.worker.js'));
	poolExecutor(async({add,multi}) => {
		console.log(await add(2,3));
		await new Promise(function(resolve){
			setTimeout(resolve,1000);
		});
		console.log(await multi(2,3));
	}).then(() => {
		console.log('任务完成2!');
	});
})();
